define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dealer/money_log/index' + location.search,
                    add_url: 'dealer/money_log/add',
                    edit_url: 'dealer/money_log/edit',
                    del_url: 'dealer/money_log/del',
                    multi_url: 'dealer/money_log/multi',
                    table: 'dealer_money_log',
                }
            });
            var table = $("#table");
            var commonClumons = [];
            if (Config.adminAuth.type === 0) {
                commonClumons = [
                    {checkbox: true},
                    {field: 'id', title: __('Id'),operate:false},
                    {field: 'dealer.name', title: '经销商',operate:false},
                    {field: 'dealer.id', 'title': '经销商id', visible:false, addClass: "selectpage", extend: "data-source='dealer/dealer/index'  data-field='name' data-order-by='createtime desc'"},
                    {field: 'money', title: __('Money'), operate:false},
                    {field: 'before', title: __('Before'), operate:false},
                    {field: 'after', title: __('After'), operate:false},
                    {field: 'memo', title: __('Memo'), operate:false},
                    {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    {field: 'operate',  title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                ];
            }
            else if(Config.adminAuth.type === 2){
                commonClumons = [
                    {checkbox: true},
                    {field: 'id', title: __('Id'),operate:false},
                    {field: 'money', title: __('Money'), operate:false},
                    {field: 'before', title: __('Before'), operate:false},
                    {field: 'after', title: __('After'), operate:false},
                    {field: 'memo', title: __('Memo'), operate:false},
                    {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                ];
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                onLoadSuccess: function (data) {
                    $('#success_money').html(data.statistics);
                 },
                columns: [
                    commonClumons
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});